package controller.trafficlights;

import simulator.enums.Direction;
import simulator.enums.LaneType;

public class CarLight extends TrafficLight
{
	private final int MAX_GREEN_TIME = 10000;
	private final int MAX_YELLOW_TIME = 3000;
	private final int MAX_WAITING_TIME = 30000;
	
	public CarLight(Direction direction, LaneType type)
	{
		_direction = direction;
		_type = type;
		_state = TrafficLightState.red;
		_isActive = false;
	}
	
	@Override
	public void updatePriority(long elapsedTime) 
	{
		if(isOccupied())
		{
			_waitingTime += elapsedTime;
			if(_waitingTime >= MAX_WAITING_TIME)
			{
				setPriority(_priority+4);
				System.out.println("Lane " + toString() + " waited too long, priority increased to: " + _priority);
				_waitingTime = 0;
			}
		}
	}
	
	@Override
	public void update(long elapsedTime) 
	{
		_elapsedTime += elapsedTime;
		
		if(_state == TrafficLightState.red)
		{
			if(isOccupied())
			{
				changeState(TrafficLightState.green);
				_elapsedTime = 0;
			}
		}
		else if(_state == TrafficLightState.green)
		{
			if(_elapsedTime >= MAX_GREEN_TIME)
			{
				changeState(TrafficLightState.yellow);
				_elapsedTime = 0;
			}
		}
		else if(_state == TrafficLightState.yellow)
		{
			if(_elapsedTime >= MAX_YELLOW_TIME)
			{
				changeState(TrafficLightState.red);
				_elapsedTime = 0;
			}
		}
	}

	@Override
	public boolean isCompatibleWith(TrafficLight trafficLight) 
	{
		Direction direction = trafficLight.getDirection();
		LaneType type = trafficLight.getLaneType();
		
		if(_direction == Direction.NORTH)
		{
			if(direction == Direction.NORTH) return true;
			if(_type == LaneType.CAR_LEFT)
			{
				if(direction == Direction.EAST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_LEFT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_RIGHT) return true;
			}
			if(_type == LaneType.CAR_RIGHT)
			{
				if(direction == Direction.EAST && type == LaneType.CAR_LEFT) return true;
				if(direction == Direction.EAST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_STRAIGHT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_STRAIGHT) return true;
			}
			if(_type == LaneType.CAR_STRAIGHT)
			{
				if(direction == Direction.EAST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_RIGHT) return true;
			}
		}
		if(_direction == Direction.EAST)
		{
			if(direction == Direction.EAST) return true;
			if(_type == LaneType.CAR_LEFT)
			{
				if(direction == Direction.NORTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_LEFT) return true;
			}
			if(_type == LaneType.CAR_RIGHT)
			{
				if(direction == Direction.NORTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.NORTH && type == LaneType.CAR_STRAIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_LEFT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_STRAIGHT) return true;
			}
			if(_type == LaneType.CAR_STRAIGHT)
			{
				if(direction == Direction.SOUTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_RIGHT) return true;
			}
		}
		if(_direction == Direction.SOUTH)
		{
			if(direction == Direction.SOUTH) return true;
			if(_type == LaneType.CAR_LEFT)
			{
				if(direction == Direction.EAST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.NORTH && type == LaneType.CAR_LEFT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_RIGHT) return true;
			}
			if(_type == LaneType.CAR_RIGHT)
			{
				if(direction == Direction.EAST && type == LaneType.CAR_LEFT) return true;
				if(direction == Direction.EAST && type == LaneType.CAR_STRAIGHT) return true;
				if(direction == Direction.NORTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.NORTH && type == LaneType.CAR_STRAIGHT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.WEST && type == LaneType.CAR_LEFT) return true;
			}
			if(_type == LaneType.CAR_STRAIGHT)
			{
				if(direction == Direction.WEST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.NORTH && type == LaneType.CAR_RIGHT) return true;
			}
		}
		if(_direction == Direction.WEST)
		{
			if(direction == Direction.WEST) return true;
			if(_type == LaneType.CAR_LEFT)
			{
				if(direction == Direction.NORTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.EAST && type == LaneType.CAR_LEFT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_RIGHT) return true;
			}
			if(_type == LaneType.CAR_RIGHT)
			{
				if(direction == Direction.NORTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.NORTH && type == LaneType.CAR_LEFT) return true;
				if(direction == Direction.EAST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.EAST && type == LaneType.CAR_STRAIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.SOUTH && type == LaneType.CAR_STRAIGHT) return true;
			}
			if(_type == LaneType.CAR_STRAIGHT)
			{
				if(direction == Direction.EAST && type == LaneType.CAR_RIGHT) return true;
				if(direction == Direction.NORTH && type == LaneType.CAR_RIGHT) return true;	
			}
		}
		return false;
	}
}
